import {Button, Image, Flex} from "@taroify/core";
import {View} from "@tarojs/components";
import axios from '@/api/interceptor'
import Taro from "@tarojs/taro";
import appIcon from '@/assets/images/virtual_teacher_icon.png'
import {setToken} from "@/utils/auth";
import './index.css';



function WxLogin() {

  const handWxLogin = async () => {
    await Taro.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          axios.post('api/v1/oauth2/wx/wx-auth', {
              code: res.code
          }).then(async (response) => {
            setToken(response.access_token)
            // 显示成功提示
            await Taro.showToast({
              title: '登录成功',
              icon: 'success',
              duration: 2000,
            });

            // 跳转到对话页面
            await Taro.redirectTo({
              url: '/pages/tabs/dialogue/index',
            });
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  }
  return (
    <View style={{ height: '100vh', width: '100wv', background: 'linear-gradient(-225deg, #2CD8D5 0%, #C5C1FF 56%, #FFBAC3 100%)' }}>
      <Flex justify='center' align='center' direction='column' style={{ height: '100%', width: '100%' }}>
        <View>
          <Image
            src={appIcon}
            style={{width: '300rpx', height: '300rpx',}}
          />
        </View>

          {/* 提交按钮 */}
        <View style={{ margin: '16px' }}>
          <Button
            shape='round'
            block
            color='primary'
            onClick={handWxLogin}
          >
            微信账号一键登录 开启智能学习体验
          </Button>
        </View>
      </Flex>
    </View>
  );
}

export default WxLogin;
